Skip to content

JS-2192 Fix S4782 false positive for external indexed access - #7694

Open
francois-mora-sonarsource wants to merge 2 commits into
masterfrom
fix-js-2192-s4782-indexed-access-external
Open

JS-2192 Fix S4782 false positive for external indexed access#7694
francois-mora-sonarsource wants to merge 2 commits into
masterfrom
fix-js-2192-s4782-indexed-access-external

Conversation

@francois-mora-sonarsource

Copy link
Copy Markdown
Contributor

Summary

Fix S4782 reporting an optional property whose type is indexed from an imported
optional property.

Root cause

The semantic classifier treated every indexed-access type as project-local,
even when the accessed property declaration came entirely from an external
library. Removing the reported ? then made the consumer property required.

Change

Resolve literal indexed-access property symbols through the TypeScript checker.
Classify the access as external only when every property declaration is from an
external or TypeScript default-library source; unresolved and local accesses
remain reportable.

Validation

  • npx tsx --test packages/analysis/tests/jsts/rules/helpers/type-origin.test.ts packages/analysis/src/jsts/rules/S4782/unit.test.ts
  • npm run bbf

@francois-mora-sonarsource
francois-mora-sonarsource requested a review from a team July 30, 2026 15:36
@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Jul 30, 2026

Copy link
Copy Markdown

JS-2192

@github-actions

Copy link
Copy Markdown
Contributor

Ruling Report

No changes to ruling expected issues in this PR

@francois-mora-sonarsource
francois-mora-sonarsource marked this pull request as ready for review July 30, 2026 15:46
@francois-mora-sonarsource
francois-mora-sonarsource requested review from a team and removed request for a team July 30, 2026 16:19
Follow-up on the indexed-access fix: an access that resolves through an
index signature rather than a named property (`ExternalRecord['anyKey']`)
was still classified as project-local, so S4782 kept reporting it and its
suggestion still made the property required.

Resolve those accesses through the index signature's own declaration, and
fall back to the declaration of the indexed type for signatures the checker
synthesizes (mapped types such as `Record<K, V>`, which have no declaration
node). Cover both shapes, plus the non-literal-index path that stays
reportable, at the helper and rule levels, and restate the shared
all-declarations-external rule and the known limitations in the docblock.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@gitar-bot

gitar-bot Bot commented Aug 4, 2026

Copy link
Copy Markdown
Code Review ✅ Approved

Classifies index-signature access by origin in S4782 to prevent false positives on external indexed access. No issues found.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing less information.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

@sonarqube-next

sonarqube-next Bot commented Aug 4, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant